import { observable, action } from 'mobx';
import { ListAdvanced, ListViewPage, SearchAdvanced,RouteLayout,Share } from 'weaver-mobile-page';
import { Tools, Toast, Bubble } from 'weaver-mobile';
import { toJS } from 'mobx';
import * as API from './../../apis/attention';
const getLabel = Tools.getLabel;
const WORKFLOW_SUPERVISE_STORE = "WORKFLOW_SUPERVISE_STORE";
export class AttentionStore {

    @observable tabs = [];

    @observable visible = false;

    @observable showCheck = false;

    @observable leftTreeValueArray = [];

    @observable needRemark = false;

    @observable remark = "";

    @observable isShowLoading = false;

    @observable flowCounts = 0;

    @observable bottomBtn = '';

    @observable nowTab = 0;

    @observable cordDatas='';

    fatherPage=""//上级页面路由

    queryParams = {}
 
    leftTree_params = {}

    isShowTotal = true;

    phrasesDatas = [];

    //确保刷新时能正确到路由页面
    pathidArr  = (window.location.hash.split('/'))
    pathid = (this.pathidArr[2] && this.pathidArr[2].includes("?")) ? this.pathidArr[2].substr(0, this.pathidArr[2].indexOf("?")) : this.pathidArr[2];

    @observable listAd_All = new ListAdvanced.Store({ route: '/attention/flowAll', id: "flowAll" });

    @observable nowListAd = this.listAd_All;

    @observable share = new Share.Store({ route: '/attention/flowAll', id: 'share' });
    msgList = [];
    shareParams = {};
    browserConfig = {};

    @action
    onSearch = (status = {}) => {
        const { searchAdvancedStore, listViewStore, searchParams } = this.nowListAd;
        // 搜索框等搜索数据
        let params = { ...searchParams };
        // 获取高级搜索参数
        if (searchAdvancedStore) {
            const { formParams } = searchAdvancedStore.form;
            params = { ...formParams, ...this.leftTree_params };
        }
        // 清除列表数据
        listViewStore.clearData();
        if (!status.notClearSelectedRowKeys) {//清空列表已选中数据
            listViewStore.setState({ selectedRowKeys: [] })
        }
        // 带参数请求列表数据
        let pathidArr  = (window.location.hash.split('/'))
        let pathid =  pathidArr[2].includes("?") ? pathidArr[2].substr(0, pathidArr[2].indexOf("?")) : pathidArr[2];
        this.nowListAd.getData({ url: '/api/workflow/requestAttention/getSessionkey', method: "POST", params: { ...params, tabKey: pathid, ismobile: 1, _ec_device: true } });
    }

    @action
    setState = (params = {}) => {
        Object.keys(params).forEach((key) => {
            this[key] = params[key];
        });
    }
    @action
    setShareMsgInfo=()=>{
        const { listViewStore } = this.nowListAd;
        const { selectedRowKeys = [] } = listViewStore;
        let title='';
        selectedRowKeys.map(val=>{
            listViewStore.data.map(v=>{
                if(v.requestid==val){
                    title = v.requestname
                }
            })
            this.msgList.push(
                {
                    content: title, // 分享标题（必填）
                    sysid: window.localStorage.emobile_ec_id, // EM中添加的集成系统的ID (必填: 按照demo给的传)
                    linkurl: "/main/workflow/req?requestid="+val+"&isovertime=0&_workflowid=2&_workflowtype=2", // 消息链接地址（必填）
                    canforward: 1, // 消息是否允许转发 1：是（默认） 0：否
                    callbackurl: "",
                    sharetypename: "流程", //消息类型显示标题，没有则按sharetype显示
                    opentype: 1, //移动端打开方式 1:app内打开（默认） 2：手机默认浏览器打开
                    opentype_pc: 2, //PC端打开方式 1:应用内打开（默认） 2：PC默认浏览器打开
                    sharetype: "workflow", // 分享数据类型："doc","workflow","task","pdoc","folder" ,"crm" 分别代表文档，流程，任务，云盘文档，云盘文件，客户名片
                    shareid: val+"", // 分享的数据ID（必填）
                }
            )
        })
        this.shareParams = {
            "resourcetype": 152,
            "resourceid": selectedRowKeys.join(','),
        }
        this.browserConfig = {
            "extendTabs": [
                {
                    "name": "",
                    "key": "",
                    "url":""
                }
            ]
        }
    }
    @action
    changeTab = (obj) => {
        let tabKey ;
        this.tabs.map((val,index)=>{
            if(val.groupid==obj.groupid){
                tabKey = index
                this.nowListAd = new ListAdvanced.Store({ route: '/attention/'+val.groupid, id: val.groupid })
            }
        })  
        // this.nowListAd = new ListAdvanced.Store({ route: '/attention/'+obj.groupid, id: obj.groupid })
        this.nowTab = tabKey;
        this.nowListAd.clear();
        this.nowListAd.getData({ url: '/api/workflow/requestAttention/getSessionkey', method: "POST", params: { tabKey:obj.groupid, ismobile: 1, _ec_device: true } });
        if (this.nowListAd.searchAdvancedStore) {
            this.nowListAd.searchAdvancedStore.getData({ url: "/api/workflow/requestAttention/getCondition", method: 'GET', params: { ...this.queryParams, ismobile: 1, _ec_device: true } })
        }
        if (this.nowListAd.listViewStore) {
            this.nowListAd.listViewStore.setState({ template: this.mobileshowtemplate })
        }
        if (this.nowListAd.leftTreeStore) {
            this.nowListAd.leftTreeStore.setState({ rowKey: "domid" })
        }
        this.showCheck = false;
        this.leftTree_params = {};
    }

//这里初始化tabS
    @action
    init = () => { 
        this.getTreeCount()  
        this.getTree()    
    }

    @action
    getTreeCount = () => {
        API.getTreeCount().then(result => {
            const { flowAllCounts = 0, flowOverCounts = 0} = result;
            if (this.nowTab == 0) {
                this.flowCounts = flowAllCounts;
            } else if (this.nowTab == 1) {
                this.flowCounts = flowAllCounts - flowOverCounts;
            } else if (this.nowTab == 2) {
                this.flowCounts = flowOverCounts;
            }
            this.isShowTotal = true;
        })
    }

    @action
    getTree = () =>{
        this.tabs = []
        // this.tabs.clear()
        API.getTree().then(result => {
            this.tabs=result.topTab
        //     result.topTab.map(val=>{
        //        this.tabs.push({title:val.title,path:val.groupid})
        })
    }



    loadPhrasesDatas = () => {
        API.loadPhrasesDatas().then(result => {
            const { phrasesDatas = [] } = result;
            this.phrasesDatas = phrasesDatas;
            this.needRemark = true;
        })
    }
    doSupervise = (params = {}) => {
        this.isShowLoading = true;
        API.doSupervise(params).then(data => {
            this.isShowLoading = false;
            this.needRemark = false;
            this.showCheck = false;
            this.remark = '';
            const { result } = data;
            if (result) {
                this.remark = "";
                this.nowListAd.clear();
                this.nowListAd.getData({ url: '/api/workflow/requestAttention/getSessionkey', method: "POST", params: { tabKey:this.nowTab, ismobile: 1, _ec_device: true } });
                if (this.nowListAd.searchAdvancedStore) {
                    this.nowListAd.searchAdvancedStore.getData({ url: "/api/workflow/requestAttention/getCondition", method: 'POST', params: { ...this.queryParams, ismobile: 1, _ec_device: true } })
                }
                if (this.nowListAd.listViewStore) {
                    this.nowListAd.listViewStore.setState({ template: this.mobileshowtemplate, selectedRowKeys: [] })
                }
                if (this.nowListAd.leftTreeStore) {
                    this.nowListAd.leftTreeStore.setState({ rowKey: "domid" })
                }
                this.showCheck = false;
                this.leftTree_params = {};
                Bubble.success(getLabel(387108,"督办成功"), 2);
            } else {
                Bubble.error(getLabel(387109,"督办失败"), 2);
            }
        })
    }
}